<script>
    import Vue from 'vue';
    import {
        getOpenIdAPI,
        loginByOpenIdAPI
    } from './api/login'
	import { 
        getArticleItemListAPI 
    } from './api/arc'

    import { getCityListAPI } from './api/address'

    const loginIn = (cb) => {
        wx.login({
            success: (res) => {
                let openId
                getOpenIdAPI({
                    code: res.code
                }).then(response => {
                    openId = response.openId
                    return loginByOpenIdAPI({
                        openId
                    })
                }).then(res => {
                    getApp().globalData.userName = res.userName
                    getApp().globalData.userIdNo = res.userIdNo
                    getApp().globalData.phoneNumber = res.phoneNumber
                    getApp().globalData.avatarUrl = res.portraitUri
                    getApp().globalData.sessionId = res.sessionId
                    getApp().globalData.openId = openId
                    wx.setStorage({
                        key: 'userInfo',
                        data: {
                            pltUserId: res.pltUserId,	
                            userIdNo: res.userIdNo,	
                            userName: res.userName,	
                            portraitUri: res.portraitUri,	
                            phoneNumber: res.phoneNumber,
                            sessionId: res.sessionId,
                        }
                    })
                    wx.setStorage({
                        key: 'openId',
                        data: openId
                    })
                    if(cb && typeof cb === 'function') cb()
                }).catch(err => {
                    getApp().globalData.openId = openId
                    wx.setStorage({
                        key: 'openId',
                        data: openId
                    })
                    console.log(err)
                })
            }
        })
    }
    export default Vue.extend({
        mpType: 'app',
        onLaunch() {
            wx.checkSession({
                success: (res) => {
                    wx.getStorage({
                        key: 'userInfo',
                        success: (res) => {
                            console.log('checkSession成功', res)
                            getApp().globalData.userName = res.data.userName
                            getApp().globalData.userIdNo = res.data.userIdNo
                            getApp().globalData.phoneNumber = res.data.phoneNumber
                            getApp().globalData.avatarUrl = res.data.avatarUrl
                            getApp().globalData.sessionId = res.data.sessionId
                        }, 
                        fail: () => {
                            loginIn()
                        }
                    })
                    wx.getStorage({
                        key: 'openId',
                        success: (res) => {
                            getApp().globalData.openId = res.data
                        }, 
                        fail: () => {
                            loginIn()
                        }
                    })
                },
                fail: (err) => {
                    loginIn()
                    console.log(err)
                }
            })

            // 获取省市县列表
            getCityListAPI().then(res => {
                getApp().globalData.addressList = res.listProvince
            })

            getArticleItemListAPI().then(res => {
                // 文章类型映射
                let articleItemMap = new Map()
                res.articleItemList.forEach(item => {
                    articleItemMap.set(item.itemId, item.itemName)
                })
                console.log(articleItemMap)
                getApp().globalData.articleItemMap = articleItemMap
			})
            console.log('App Launch')
        },
        onShow() {
            console.log('App Show')
            let router = this.$u
            this.globalData.toLogin = function() {
           	    router.route('pages/login/login')
            }
        },
        onHide() {
            console.log('App Hide')
        },
        globalData: {
            loginIn
        }
    });
</script>

<style lang="scss">
@import "uview-ui/index.scss";
    /*每个页面公共css */
</style>
